package org.dreamwork.jasmine2.sample;

import org.dreamwork.i18n.LocaleWarp;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.events.IChangeListener;
import org.dreamwork.jasmine2.events.IClickListener;
import org.dreamwork.jasmine2.web.IWebContainer;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.web.controls.*;
import org.dreamwork.jasmine2.web.html.Table;
import org.dreamwork.jasmine2.web.html.TableCell;
import org.dreamwork.jasmine2.web.html.TableRow;
import org.dreamwork.util.ListDataCollection;

import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-2-3
 * Time: 13:34:24
 */
public class FirstPage extends Page {
    protected TextBox text1, text2, text3;
    protected ListDataCollection<Item> list = new ListDataCollection<Item> ();
    protected Button button1;
    protected Pagging2 pagging1;
    protected Repeater repeater3;
    protected DropdownList DropdownList1;
    protected Label label_1;
    protected Panel panel1;
    protected CheckBox checkbox1;
    protected DropdownList locales;

    protected ColorItem blue = new ColorItem ("blue", "blue");

    @Override
    public void init () {
        super.init ();


    }

    @Override
    public void onPagePreload (Page page) throws EventException {
        TestChanger changer = new TestChanger ();
        MyClickListener clicker = new MyClickListener ();
        button1.addClickListener (clicker);
        checkbox1.addClickListener (clicker);
        text2.addChangeListener (changer);
        text3.addChangeListener (changer);
        DropdownList1.addChangeListener (changer);

        locales.addChangeListener (new IChangeListener () {
            public void onChange (IWebControl sender, String attrName, Object oldValue, Object newVlaue) throws EventException {
                languageChanged ();
/*
                String[] a = newVlaue.toString ().split ("_");
                locale = new Locale (a[0], a[1]);
                session.setAttribute (IWebControl.LOCALE_KEY, locale);
                locales.setSelectedValue (locale);
*/
            }
        });
    }

    @Override
    public void onPageLoad (Page page) throws EventException {
        int pageNo = pagging1.getPageNo ();
        int pageSize = pagging1.getPageSize ();

        list.setTotalRows (57);
        list.setPageNo (pageNo);
        list.setPageSize (pageSize);
        int start = (pageNo - 1) * pageSize;
        pagging1.setTotalRows (list.getTotalRows ());

        for (int i = start; i < pageSize + start && i < list.getTotalRows (); i ++) {
            Item item = new Item ();
            item.setId (i);
            item.setTitle ("this is item " + i);
            list.add (item);
        }

        repeater3.setDataSource (list);
    }

    @Override
    public void onPageLoadCompleted (Page page) throws EventException {
        try {
            dataBind ();
        } catch (Throwable t) {
            t.printStackTrace ();
            throw new EventException (t);
        }
    }

    private class TestChanger implements IChangeListener {
        public void onChange (IWebControl sender, String attrName, Object oldValue, Object newValue) throws EventException {
            String s = "sender = " + sender.getId () + ", attrName = " + attrName + ", oldValue = " + oldValue + ", newValue = " + newValue;
            label_1.setText (s);
            label_1.setStyle ("color", (String) DropdownList1.getSelectedValue ());
        }
    }

    protected java.util.List<ColorItem> colorList = Arrays.asList (
            new ColorItem ("red", "red"),
            new ColorItem ("green", "green"),
            blue,
//            new ColorItem ("blue", "blue"),
            new ColorItem ("yellow", "yellow")            
    );

    private class MyClickListener implements IClickListener {
        public void onClick (IWebControl sender) throws EventException {
            text2.setText (text1.getText ());
            IWebContainer form = getForm ();
            if (form != null) {
                form.addControl (new Literal ("HELLO@"));
            }
            button1.setText ("别看我只是一个按钮!");
            createTable ();
        }
    }



    private void createTable () {
        Table table = new Table ();
        table.setPage (this);
        table.setAttribute ("bgcolor", "#CCCCCC");
        table.setAttribute ("cellpadding", "3");
        table.setAttribute ("cellspacing", "1");
        for (int i = 0; i < 5; i ++) {
            TableRow row = new TableRow ();
            row.setAttribute ("bgcolor", "white");
            row.setPage (this);
            table.addRow (row);
            for (int j = 0; j < 5; j ++) {
                TableCell cell = new TableCell ();
                if (i % 2 != j % 2)
                    cell.setAttribute ("bgcolor", "#EFEFEF");
                Label label = new Label ();
                label.setViewStateEnabled (false);
                label.setText ("Cell(" + i + ", " + j + ")");
                cell.addControl (label);
                cell.setPage (this);
                row.addCell (cell);
            }
        }

        panel1.addControl (table);
    }

    private void languageChanged () {
        LocaleWarp warp = (LocaleWarp) locales.getSelectedItem ();
        locale = warp.getLocale ();
        session.setAttribute (IWebControl.LOCALE_KEY, locale);
    }
}